package ccm.component.menu;

import ccm.common.Buffer;
import ccm.common.CCMConfigManager;
import ccm.component.override.JButton;
import ccm.component.override.JCheckBox;
import ccm.component.override.JLabel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class BooleanMenuItemJPanel extends SingleMenuItem implements ActionListener, ValuableMenuItem
{
    final protected JLabel valueJLabel;
    final protected JCheckBox valueJCheckBox;
    final protected JButton sendJButton;
    protected Boolean value;

    protected BooleanMenuItemJPanel(DelegateSenderInterface sender,Component[] rightMenu,String saveArgPrefix,int dataIndex,boolean canModify,ScnsType type,int page,String name)
    {
        super(sender,rightMenu,saveArgPrefix,dataIndex,canModify,type,page,name);
        valueJLabel=new JLabel();
        valueJLabel.setOpaque(true);
        add(valueJLabel);
        valueJCheckBox=new JCheckBox("true");
        add(valueJCheckBox);
        sendJButton=new JButton("发送");
        add(sendJButton);
        sendJButton.addActionListener(this);
        loadConfig();
        valueJCheckBox.addChangeListener(e->storeConfig());
        valueJCheckBox.addActionListener(this);
        setCanModify(canModify);
        Dimension dimension=new Dimension(100,(int)(valueJLabel.getFont().getSize()*1.2+10));
        valueJLabel.setPreferredSize(dimension);
        valueJCheckBox.setPreferredSize(dimension);
        valueJLabel.setBorder(BorderFactory.createEtchedBorder());
        valueJCheckBox.setBorder(BorderFactory.createEtchedBorder());
    }

    @Override
    protected String storeConfig()
    {
        final String prefix=super.storeConfig();
        CCMConfigManager.setBoolean(prefix+"/value",valueJCheckBox.isSelected());
        return prefix;
    }

    @Override
    public String loadConfig()
    {
        final String prefix=super.loadConfig();
        if(valueJCheckBox!=null)
        {
            valueJCheckBox.setSelected(CCMConfigManager.setBooleanIfAbsent(prefix+"/value",false));
            valueJCheckBox.setText(String.valueOf(valueJCheckBox.isSelected()));
            updateColor();
        }
        return prefix;
    }

    @Override
    public void setCanModify(boolean canModify)
    {
        super.setCanModify(canModify);
        if(valueJCheckBox!=null)
        {
            valueJCheckBox.setVisible(canModify);
        }
        if(sendJButton!=null)
        {
            sendJButton.setVisible(canModify);
        }
    }

    protected void updateColor()
    {
        if(value!=null)
        {
            valueJLabel.setText(((Boolean)value).toString());
        }
        if(isCanModify()&&!sameAsValue(valueJCheckBox.isSelected()))
        {
            valueJLabel.setForeground(Color.RED);
        }
        else
        {
            valueJLabel.setForeground(null);
        }
    }

    protected boolean sameAsValue(Object v)
    {
        return ((Boolean)v)==value;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        valueJCheckBox.setText(String.valueOf(valueJCheckBox.isSelected()));
        updateColor();
        if(e.getSource()==sendJButton||autoSendJCheckBox.isSelected())
        {
            needSendValueFlag=true;
        }
    }

    @Override
    public void decodeValueAndSet(byte[] receiveBuf,int start)
    {
        Boolean value=Buffer.getUint8(receiveBuf,start)!=0;
        if(this.value==null||!sameAsValue(value))
        {
            valueJLabel.setBackground(Color.YELLOW);
        }
        else
        {
            valueJLabel.setBackground(null);
        }
        this.value=value;
        updateColor();
    }

    @Override
    public void loadValueToInput()
    {
        valueJCheckBox.setSelected(value);
        updateColor();
        storeConfig();
    }

    @Override
    public void sendValueToDevice()
    {
        int start=0;
        byte[] buf=new byte[64];
        start=Buffer.setUint16(buf,start,dataIndex);
        start=Buffer.setUint8(buf,start,type.ordinal());
        start=Buffer.setUint8(buf,start,valueJCheckBox.isSelected()?1:0);
        addTailAndSend(buf,start,0X20);
        needSendValueFlag=false;
    }

    @Override
    public void requestValue()
    {
        System.out.println("requestValue(0X1E) "+dataIndex);
        int start=0;
        byte[] buf=new byte[64];
        start=Buffer.setUint16(buf,start,dataIndex);
        addTailAndSend(buf,start,0X1E);
    }

    @Override
    public String toString()
    {
        return value.toString();
    }
}
